using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Linq;

namespace SlamCms.SharePoint.Features.SlamCMS_Branding
{
	/// <summary>
	/// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
	/// </summary>
	/// <remarks>
	/// The GUID attached to this class may be used during packaging and should not be modified.
	/// </remarks>

	[Guid("f6529128-e2f5-46b2-9d62-136ba012e226")]
	public class SlamCMS_BrandingEventReceiver : SPFeatureReceiver
	{
		public override void FeatureActivated(SPFeatureReceiverProperties properties)
		{
			var site = properties.Feature.Parent as SPSite;

			if (site != null)
			{
				foreach (SPWeb web in site.AllWebs)
				{
					var feature = web.Features.FirstOrDefault(o => o.DefinitionId.Equals(new Guid("4b844ea5-e9a1-4451-98fa-b49eadd18865")));

					if (feature == null)
					{
						web.Features.Add(new Guid("4b844ea5-e9a1-4451-98fa-b49eadd18865"), true);
					}
				}
			}
		}

		public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
		{
			var site = properties.Feature.Parent as SPSite;

			if (site != null)
			{
				foreach (SPWeb web in site.AllWebs)
				{
					var feature = web.Features.FirstOrDefault(o => o.DefinitionId.Equals(new Guid("4b844ea5-e9a1-4451-98fa-b49eadd18865")));

					if (feature != null)
					{
						web.Features.Remove(feature.DefinitionId);
					}
				}
			}
		}
	}
}
